package com.joesem.sale.client.logic.workOrder

import android.os.Bundle
import android.util.Log
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import com.jiaxing.sale.client.R
import com.jiaxing.sale.client.databinding.FragmentOrderContentBinding
import com.joesem.sale.client.base.BaseFragment
import com.joesem.sale.client.base.createViewModel
import com.joesem.sale.client.base.launch
import com.joesem.sale.client.db.LoginTable
import com.joesem.sale.client.logic.workOrder.WorkOrderFragment.Filter
import com.joesem.sale.client.logic.workOrder.viewModel.WorkOrderViewModel

class OrderContentFragment : BaseFragment<FragmentOrderContentBinding>() {

    private val viewModel by createViewModel<WorkOrderViewModel>()

    private val adapter = WorkOrderAdapter()

    private var status = ""
    override fun FragmentOrderContentBinding.initView(savedInstanceState: Bundle?) {
        rvContent.adapter = adapter
    }

    override fun FragmentOrderContentBinding.initListener() {
        adapter.itemClick = {
            launch<WorkOrderDetailsActivity>("orderId" to it.id.toString())
        }
        lifecycle.addObserver(object : DefaultLifecycleObserver {
            override fun onCreate(owner: LifecycleOwner) {

            }

            override fun onResume(owner: LifecycleOwner) {
                status = arguments?.getString("type").orEmpty()
                Log.i("打印", "onResume: $status")
                if (LoginTable.table.isLogin())
                    srlRefresh.autoRefresh()
                Filter.filterChange = {
                    if (LoginTable.table.isLogin())
                        srlRefresh.autoRefresh()
                }
            }
        })
        srlRefresh.apply {
            setOnRefreshListener {
                if (LoginTable.table.isLogin())
                    viewModel.getOrderList(
                        date_type = Filter.date_type,
                        date_end = Filter.date_end,
                        date_start = Filter.date_start,
                        status = status,
                        about_me = Filter.about_me,
                        isFirst = true
                    )
            }
            setOnLoadMoreListener {
                if (LoginTable.table.isLogin())
                    viewModel.getOrderList(
                        date_type = Filter.date_type,
                        date_end = Filter.date_end,
                        date_start = Filter.date_start,
                        status = status,
                        about_me = Filter.about_me,
                        isFirst = false
                    )
            }
        }
    }

    override fun FragmentOrderContentBinding.initIntent(intent: Any?) {
        when (intent) {
            is WorkOrderViewModel.ListDataIntent -> {
                srlRefresh.finishRefresh()
                srlRefresh.finishLoadMore()
                if (intent.isFirst) {
                    adapter.setList(intent.data)
                    if (adapter.data.isEmpty()) {
                        adapter.setEmptyView(R.layout.layout_empty)
                    }
                } else {
                    adapter.addData(intent.data)
                }
            }
        }
    }
}